package com.vaeke.app.sina;

import static com.vaeke.app.sina.Constant.accessToken;
import static com.vaeke.app.sina.Constant.accessTokenSecret;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.WeiboException;

import com.vaeke.app.sina.focus.Friends;
import com.vaeke.app.sina.index.GetFriendsTimeline;

public class Main extends JFrame {

    private static final long serialVersionUID      = 4197219538001089345L;

    private static Main       mainFrame             = new Main();
    // focus list
    private JList             friendList;
    // index page's newst weibo
    private JList             indexPageMsgs;
    Font                      friendListDisplayFont = new Font("Serif", Font.BOLD, 16);

    public static void main(String[] args) {
        mainFrame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        mainFrame.setTitle("vaeke");
        int width = 350, height = 600;
        mainFrame.setSize(width, height);
        mainFrame.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - width) / 2,
                              (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - height) / 2);
        mainFrame.initFrame();
    }

    private void initFrame() {
        Container container = mainFrame.getContentPane();

        /** init tab label */
        // 1. index page's tab
        JPanel indexPanel = new JPanel();
        // 2. send weibo's tab
        JPanel sendMsgPanel = new JPanel();
        // 3. focus tab
        JPanel friendListPanel = new JPanel();
        // 4. fllows tab
        JPanel followListPanel = new JPanel();

        // index pages' tab show newst weibo
        GetFriendsTimeline getFriendTimeline = new GetFriendsTimeline();
        List<Status> statuses = getFriendTimeline.getFriendsTimeline(true, new String[] { accessToken,
                accessTokenSecret });
        List<String> weiboList = new ArrayList<String>();
        for (Status status : statuses) {
            weiboList.add(status.getUser().getName() + " " + status.getText() + " " + status.getCreatedAt().getTime());
        }
        indexPageMsgs = new JList(weiboList.toArray());
        JScrollPane indexPageMsgScrollPane = new JScrollPane(indexPageMsgs);
        indexPageMsgScrollPane.setBackground(Color.WHITE);

        // send weibo
        final JTextArea msgField = new JTextArea(5, 20);
        final JLabel showSendResult = new JLabel();
        msgField.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e) {
                showSendResult.setText("");
            }

            @Override
            public void focusLost(FocusEvent e) {
                // TODO Auto-generated method stub
            }
        });

        JButton btn = new JButton("send");
        msgField.setBackground(Color.LIGHT_GRAY);
        JPanel sendFunctionPanel = new JPanel();
        sendFunctionPanel.add(msgField);
        sendFunctionPanel.add(showSendResult);
        sendFunctionPanel.add(btn, BorderLayout.SOUTH);
        sendMsgPanel.add(sendFunctionPanel, BorderLayout.CENTER);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Weibo weibo = new Weibo("zxkletters@gmail.com", "zhangxiaoke");
                try {
                    Status status = weibo.updateStatus(msgField.getText());
                    msgField.setText("");
                    showSendResult.setText("send success!");
                } catch (WeiboException e1) {
                    e1.printStackTrace();
                }
            }

        });

        // my focus tab
        Friends friends = new Friends();
        friendList = new JList(friends.getFriendNames(accessToken, accessTokenSecret).toArray());
        friendList.setFont(friendListDisplayFont);
        friendList.addListSelectionListener(new ValueReporter());
        JScrollPane friendListScrollPane = new JScrollPane(friendList);
        friendListScrollPane.setBackground(Color.WHITE);

        indexPanel.add(indexPageMsgScrollPane);
        friendListPanel.add(friendListScrollPane);

        // tab's main panel
        JTabbedPane mainTabedPanel = new JTabbedPane();
        mainTabedPanel.addTab("index", indexPanel);
        mainTabedPanel.setSelectedIndex(0);
        mainTabedPanel.addTab("send weibo", sendMsgPanel);
        mainTabedPanel.addTab("my focus", friendListPanel);
        mainTabedPanel.addTab("my follows", followListPanel);

        // JLabel valueLabel = new JLabel("send to");
        // sendToLabel = new JLabel("");
        // valueLabel.setFont(friendListDisplayFont);
        // sendToLabel.setFont(friendListDisplayFont);
        // // valueField = new JTextField("None", 7);
        // final JTextField valueField2Self = new JTextField(
        // "this message from weeke!");
        //
        // // valueField.setFont(displayFont);
        // valueField2Self.setFont(friendListDisplayFont);
        //
        // JPanel valuePanel = new JPanel();
        // valuePanel.setBackground(Color.white);
        // Border valuePanelBorder = BorderFactory
        // .createTitledBorder("send message");
        // valuePanel.setBorder(valuePanelBorder);
        // valuePanel.add(valueLabel);
        // valuePanel.add(sendToLabel);
        // // valuePanel.add(valueField);
        //
        // JPanel valuePanel2Self = new JPanel();
        // valuePanel2Self.setBackground(Color.white);
        //
        // valuePanel2Self.setBorder(valuePanelBorder);
        // // valuePanel2Self.add(valueLabel);
        // valuePanel2Self.add(valueField2Self);
        // JButton btn = new JButton("send");
        // btn.addActionListener(new ActionListener() {
        //
        // @Override
        // public void actionPerformed(ActionEvent e) {
        // Weibo weibo = new Weibo("zxkletters@gmail.com", "zhangxiaoke");
        // // String msg = "this message from weeke!";
        // try {
        // Status status = weibo.updateStatus(valueField2Self
        // .getText());
        // } catch (WeiboException e1) {
        // e1.printStackTrace();
        // }
        // }
        //
        // });
        // valuePanel2Self.add(btn);

        // mainFrame.add(valuePanel, BorderLayout.CENTER);
        // mainFrame.add(valuePanel2Self, BorderLayout.SOUTH);

        // mainFrame.add(friendListPanel, BorderLayout.BEFORE_FIRST_LINE);
        container.add(mainTabedPanel, BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }

    protected JPanel createInnerPanel(String text) {
        JPanel jplPanel = new JPanel();
        JLabel jlbDisplay = new JLabel(text);
        jlbDisplay.setHorizontalAlignment(JLabel.CENTER);
        jplPanel.setLayout(new GridLayout(1, 1));
        jplPanel.add(jlbDisplay);
        return jplPanel;
    }

    private class ValueReporter implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent event) {
            // if (!event.getValueIsAdjusting())
            // sendToLabel.setText(friendList.getSelectedValue().toString());
        }
    }
}
